package com.party.admin.biz.refund;

import com.party.core.model.activity.ActStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.activity.ActivityType;
import com.party.core.model.goods.GoodsCoupons;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberAct;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.order.OrderForm;
import com.party.core.model.order.OrderType;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.goods.IGoodsCouponsService;
import com.party.core.service.member.IMemberActService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.order.IOrderFormService;
import com.party.notify.notifyPush.officialPartner.IOfficialPartnerNotifySendService;
import com.party.notify.notifyPush.servce.INotifySendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单消息推送
 * Created by wei.li
 *
 * @date 2017/3/31 0031
 * @time 19:22
 */
@Service
public class MessageOrderBizService {

	@Autowired
	private IOrderFormService orderFormService;

	@Autowired
	private INotifySendService notifySendService;

	@Autowired
	private IGoodsCouponsService goodsCouponsService;

	@Autowired
	private IMemberActService memberActService;

	@Autowired
	private IMemberService memberService;

	@Autowired
	private IMemberGroupService memberGroupService;

	@Autowired
	private IActivityService activityService;

	@Autowired
	private IOfficialPartnerNotifySendService officialPartnerNotifySendService;

	/**
	 * 活动审核拒绝推送
	 *
	 * @param orderId 订单编号
	 */
	public void activityRefundSend(String orderId) {
		OrderForm orderForm = orderFormService.get(orderId);
		if (orderForm.getPayment() > Float.valueOf("0.0")) {
			notifySendService.sendPayCheack(orderForm);
		} else {
			notifySendService.sendFreeCheack(orderForm);
		}
	}

	/**
	 * 免费活动审核拒绝推送
	 *
	 * @param memberActId 活动报名id
	 * @param activityId  活动id
	 * @param memberId    用户id
	 */
	public void freeActivityRejectSend(String memberActId, String activityId, String memberId) {
		MemberAct memberAct = memberActService.get(memberActId);
		OrderForm orderForm = new OrderForm();
		orderForm.setMemberId(memberId);
		orderForm.setGoodsId(activityId);
		orderForm.setId(memberActId);
		orderForm.setStatus(ActStatus.ACT_STATUS_AUDIT_REJECT.getCode());
		orderForm.setPhone(memberAct.getMobile());
		Activity activity = activityService.get(activityId);
		Integer activityType = activityService.getActivityType(activity);
		if (ActivityType.COURSE.getCode().equals(activityType)) {
			orderForm.setType(OrderType.ORDER_COURSE.getCode());
		} else if (ActivityType.ACTIVITY.getCode().equals(activityType)) {
			orderForm.setType(OrderType.ORDER_ACTIVITY.getCode());
		}
		notifySendService.sendFreeCheack(orderForm);
	}

	/**
	 * 退款推送
	 * 
	 * @param orderId 订单编号
	 */
	public void refundSend(String orderId) {
		OrderForm orderForm = orderFormService.get(orderId);
		if (orderForm.getType().equals(OrderType.ORDER_ACTIVITY.getCode())) {
			notifySendService.sendActivityRefund(orderForm);
		} else if (orderForm.getType().equals(OrderType.ORDER_NOMAL.getCode())
				|| orderForm.getType().equals(OrderType.ORDER_CUSTOMIZED.getCode())) {
			notifySendService.sendGoodsRefund(orderForm);
		} else if (orderForm.getType().equals(OrderType.ORDER_COURSE.getCode())) {
			// todo 课程的退款
		}
	}

	/**
	 * 下单推送推送
	 * 
	 * @param orderId 订单号
	 */
	public void paySend(String orderId) {
		OrderForm orderForm = orderFormService.get(orderId);
		GoodsCoupons goodsCoupon = new GoodsCoupons();
		goodsCoupon.setOrderId(orderForm.getId());
		List<GoodsCoupons> goodsCoupons = goodsCouponsService.list(goodsCoupon);
		for (GoodsCoupons coupons : goodsCoupons) {
			if (orderForm.getType().equals(OrderType.ORDER_ACTIVITY.getCode())) {
				Member buyerMember = memberService.get(orderForm.getMemberId());
				MemberGroup orgMember = memberGroupService.get(orderForm.getInitiatorId());
				Activity activity = activityService.get(orderForm.getGoodsId());
				notifySendService.sendActivityApply(orderForm, coupons.getVerifyCode(), activity, orgMember);

				officialPartnerNotifySendService.sendActivityApply(buyerMember.getRealname(), orderForm.getTitle(),
						orderForm.getGoodsId(), orgMember.getMobile(), orgMember.getId());

			} else if (orderForm.getType().equals(OrderType.ORDER_NOMAL.getCode())
					|| orderForm.getType().equals(OrderType.ORDER_CUSTOMIZED.getCode())) {
				notifySendService.senGoodsBuy(orderForm, coupons.getVerifyCode());
			}
		}
	}

	/**
	 * 免费活动推送
	 * @param type 类型
	 * @param goodsId 活动/玩法ID
	 * @param businessId 活动报名/玩法预定ID
	 * @param memberId 当前用户ID
	 */
	public void sendFree(Integer type, String goodsId, String businessId, String memberId) {
		GoodsCoupons t = new GoodsCoupons();
		t.setGoodsId(goodsId);
		t.setMemberId(memberId);
		List<GoodsCoupons> goodsCoupons = goodsCouponsService.list(t);
		MemberAct memberAct = memberActService.get(businessId);
		Activity activity = activityService.get(memberAct.getActId());
		Member member = memberService.get(memberAct.getMemberId());
		MemberGroup builder = memberGroupService.get(activity.getMember());
		for (GoodsCoupons coupons : goodsCoupons) {
			if (type.equals(OrderType.ORDER_ACTIVITY.getCode())) {
				notifySendService.sendActivityApplyFree(goodsId, businessId, coupons.getVerifyCode());
				officialPartnerNotifySendService.sendActivityApply(member.getRealname(),
						activity.getTitle(), activity.getId(),
						builder.getMobile(), builder.getId());
			}
		}
	}
}
